<template>
	<view class="person-content">
		<view class="tips-box">
			注：如果未填写银行信息将无法进行提现！
		</view>
		<view class="person-box">
			<u-form :model="form" ref="uForm" label-width="auto">
				<u-form-item label="昵称"><u-input v-model="form.nickName" input-align="right"
						placeholder='请输入昵称' /></u-form-item>
				<u-form-item label="真实姓名"><u-input v-model="form.realName" input-align="right"
						placeholder='请输入真实姓名' /></u-form-item>
				<u-form-item label="性别"><u-input v-model="form.sexLable" type="select" @click="sexShow = true"
						input-align="right" placeholder='请选择性别' /></u-form-item>
				<u-form-item label="手机号"><u-input v-model="form.phone" input-align="right"
						placeholder='请输入手机号' /></u-form-item>
				<u-form-item label="微信号"><u-input v-model="form.weChatSignal" input-align="right"
						placeholder='请输入微信号' /></u-form-item>
				<u-form-item label="银行"><u-input v-model="form.bankName" type="select" @click="bankShow = true"
						input-align="right" placeholder='请选择银行' /></u-form-item>
				<u-form-item label="预留姓名"><u-input v-model="form.bankRealName" input-align="right"
						placeholder='请输入银行卡预留姓名' /></u-form-item>
				<u-form-item label="银行卡号"><u-input v-model="form.bankNo" input-align="right"
						placeholder='请输入银行卡银行卡号' /></u-form-item>
			</u-form>
			<u-select v-model="sexShow" :list="sexList" @confirm="confirm"></u-select>
			<u-select v-model="bankShow" :list="bankList" value-name='value' label-name='name'
				@confirm="bankConfirm"></u-select>
		</view>
		<view class="submit-btn">
			<u-button type="primary" shape="circle" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		ChangeUserInfo,
		GetBankList,
		AddOrUpdateBankInfo
	} from "@/api/index.js"
	import {
		getUserInfo
	} from "@/api/login.js"
	export default {
		data() {
			return {
				sexShow: false,
				bankShow: false,
				sexList: [{
						value: 0,
						label: '未知'
					}, {
						value: 1,
						label: '男'
					},
					{
						value: 2,
						label: '女'
					}
				],
				bankList: [],
				form: {
					openId: '',
					avatar: '',
					sex: '',
					sexLable: '',
					nickName: '',
					realName: '',
					phone: '',
					weChatSignal: '',
					bankName: '',
					bankRealName: '',
					bankNo: '',
					bankCode: ''
				}
			}
		},
		onLoad() {
			this.getUserInfo()
			this.getBankList()
		},
		methods: {
			// 获取银行卡列表
			getBankList() {
				GetBankList({
					typeCode: 'bank'
				}).then(res => {
					this.bankList = res.data
				})
			},
			// 获取当前用户信息
			getUserInfo() {
				this.form = uni.getStorageSync('userInfo')
				this.form.sex == 0 ? this.form.sexLable = '未知' : this.form.sex == 1 ? this.form.sexLable = '男' : this.form
					.sexLable = '女'
			},
			confirm(e) {
				console.log(e);
				this.form.sexLable = e[0].label
				this.form.sex = e[0].value
			},
			bankConfirm(e) {
				console.log(e);
				this.form.bankCode = e[0].value
				this.form.bankName = e[0].label
			},
			async submit() {
				let that = this
				let query = {
					openId: uni.getStorageSync('userInfo').openId,
					bankRealName: that.form.bankRealName,
					bankName: that.form.bankName,
					bankCode: that.form.bankCode,
					bankNo: that.form.bankNo
				}
				await AddOrUpdateBankInfo(query)
				await ChangeUserInfo(this.form)
				const res = await getUserInfo({
					openId: uni.getStorageSync('userInfo').openId
				})
				uni.setStorageSync('userInfo', res.data)
				uni.showToast({
					title: "修改成功！",
					duration: 800
				})
				setTimeout(() => {
					// uni.switchTab({
					// 	url: '/pages/mine/index'
					// })
					uni.navigateBack()
				}, 1000)
			}
		}
	}
</script>

<style lang="scss">
	.person-box {
		width: 95%;
		margin: 20rpx auto;
		background-color: #fff;
		padding: 15rpx;
		border-radius: 10rpx;

		.tips-box {
			padding: 20rpx 10rpx;
			background-color: rgb(123, 219, 255);
			color: rgb(16, 63, 145);
			font-size: 24rpx;
		}
	}

	.submit-btn {
		width: 95%;
		position: fixed;
		bottom: 30rpx;
		left: 2.5%;

	}
</style>